#include "checkview.h"
#include "employeemanager.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QDateTime>
#include <QDebug>

CheckView::CheckView(EmployeeManager *manager, const QString &empId, QWidget *parent) : QWidget(parent),
    m_employeeManager(manager),
    m_empId(empId),
    m_checkInButton(nullptr),
    m_checkOutButton(nullptr),
    m_statusLabel(nullptr),
    m_checkInTimeLabel(nullptr),
    m_checkOutTimeLabel(nullptr),
    m_attendanceStatusLabel(nullptr)
{
    initUI();
    refreshAttendanceRecord();

    // 连接信号槽
    connect(m_checkInButton, &QPushButton::clicked, this, &CheckView::onCheckInButtonClicked);
    connect(m_checkOutButton, &QPushButton::clicked, this, &CheckView::onCheckOutButtonClicked);
    if (m_employeeManager) {
        connect(m_employeeManager, &EmployeeManager::checkStatusUpdated, this, &CheckView::updateCheckStatus);
    }
}

CheckView::~CheckView()
{

}

void CheckView::initUI()
{
    // 设置窗口标题
    setWindowTitle("员工打卡");
    setMinimumSize(400, 300);

    // 创建主布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    // 创建状态标签
    m_statusLabel = new QLabel("欢迎使用员工打卡系统");
    m_statusLabel->setAlignment(Qt::AlignCenter);
    m_statusLabel->setStyleSheet("color: blue; font-size: 16px;");
    mainLayout->addWidget(m_statusLabel);

    // 创建打卡记录区域
    QWidget *recordWidget = new QWidget;
    QVBoxLayout *recordLayout = new QVBoxLayout(recordWidget);

    m_checkInTimeLabel = new QLabel("签到时间: ");
    m_checkOutTimeLabel = new QLabel("签退时间: ");
    m_attendanceStatusLabel = new QLabel("考勤状态: ");

    recordLayout->addWidget(m_checkInTimeLabel);
    recordLayout->addWidget(m_checkOutTimeLabel);
    recordLayout->addWidget(m_attendanceStatusLabel);

    mainLayout->addWidget(recordWidget);

    // 创建按钮区域
    QWidget *buttonWidget = new QWidget;
    QHBoxLayout *buttonLayout = new QHBoxLayout(buttonWidget);

    m_checkInButton = new QPushButton("签到");
    m_checkOutButton = new QPushButton("签退");

    // 设置按钮样式
    m_checkInButton->setStyleSheet("background-color: green; color: white; font-size: 14px; padding: 8px 16px;");
    m_checkOutButton->setStyleSheet("background-color: orange; color: white; font-size: 14px; padding: 8px 16px;");

    buttonLayout->addWidget(m_checkInButton);
    buttonLayout->addWidget(m_checkOutButton);

    mainLayout->addWidget(buttonWidget);

    // 添加拉伸项
    mainLayout->addStretch();

    setLayout(mainLayout);
}

void CheckView::onCheckInButtonClicked()
{
    if (m_employeeManager) {
        m_employeeManager->checkIn(m_empId);
        // 刷新打卡记录
        refreshAttendanceRecord();
    }
}

void CheckView::onCheckOutButtonClicked()
{
    if (m_employeeManager) {
        m_employeeManager->checkOut(m_empId);
        // 刷新打卡记录
        refreshAttendanceRecord();
    }
}

void CheckView::updateCheckStatus(const QString &status)
{
    m_statusLabel->setText(status);
    // 根据状态改变标签颜色
    if (status.contains("成功")) {
        m_statusLabel->setStyleSheet("color: green; font-size: 16px;");
    } else if (status.contains("迟到") || status.contains("早退")) {
        m_statusLabel->setStyleSheet("color: orange; font-size: 16px;");
    } else {
        m_statusLabel->setStyleSheet("color: red; font-size: 16px;");
    }
}

void CheckView::refreshAttendanceRecord()
{
    if (m_employeeManager) {
        QString currentDate = QDateTime::currentDateTime().toString("yyyy-MM-dd");
        QVariantList record = m_employeeManager->getAttendanceRecord(m_empId, currentDate);

        if (!record.isEmpty()) {
            QString checkInTime = record[0].toString();
            QString checkOutTime = record[1].toString();
            QString status = record[2].toString();

            m_checkInTimeLabel->setText(QString("签到时间: %1").arg(checkInTime.isEmpty() ? "未签到" : checkInTime));
            m_checkOutTimeLabel->setText(QString("签退时间: %1").arg(checkOutTime.isEmpty() ? "未签退" : checkOutTime));
            m_attendanceStatusLabel->setText(QString("考勤状态: %1").arg(status));

            // 根据打卡状态更新按钮状态
            m_checkInButton->setEnabled(checkInTime.isEmpty());
            m_checkOutButton->setEnabled(!checkInTime.isEmpty() && checkOutTime.isEmpty());
        } else {
            m_checkInTimeLabel->setText("签到时间: 未签到");
            m_checkOutTimeLabel->setText("签退时间: 未签退");
            m_attendanceStatusLabel->setText("考勤状态: 未打卡");

            m_checkInButton->setEnabled(true);
            m_checkOutButton->setEnabled(false);
        }
    }
}
